package modelLayer;

import java.util.ArrayList;

public class ProductContainer {
    
    private ArrayList<Product> products;
    private static ProductContainer instance;
   
    private ProductContainer() {
        products = new ArrayList<>();
    }
    
    public static ProductContainer getInstance() {
        if (instance == null) {
            instance = new ProductContainer();
        }
        
        return instance;
    }
    
    public void addProduct(Product product) {
        products.add(product);
    }
    
    public Product findProduct(int barcode) {
        boolean found = false;
        int index = 0;
        while(index < products.size() && !found) {
            Product prd = products.get(index);
            if(prd.getBarcode()==(barcode)) {
                found = true;
            }
            else {
                index++;
            }
        }
        
        if(found) {
            return products.get(index);
        }
        else {
            return null;
        }
    }
    
    public void removeProduct(Product prod) {
        products.remove(prod);
    }
    
    public int numberOfProducts(){
     return products.size();
    }
    
    public double totalStock() { 
        double stock = 0;
        for(Product prod : products) {
            stock += prod.getPrice() * prod.getInStock();
        }
        return stock;
    }
}
    

